package com.cloud.pole.shop.service.order.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cloud.pole.api.user.BaseUserApi;
import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.exception.BaseException;
import com.cloud.pole.common.msg.ApiRestResponse;
import com.cloud.pole.common.util.jwt.IJWTInfo;
import com.cloud.pole.common.util.jwt.JWTInfo;
import com.cloud.pole.common.util.jwt.JwtTokenUtil;
import com.cloud.pole.feign.IUserService;
import com.cloud.pole.shop.biz.goods.ShopGoodsBiz;
import com.cloud.pole.shop.biz.order.ShopOrderBiz;
import com.cloud.pole.shop.biz.order.ShopOrderCommentBiz;
import com.cloud.pole.shop.biz.order.ShopOrderItemsBiz;
import com.cloud.pole.shop.model.entity.goods.ShopGoods;
import com.cloud.pole.shop.model.entity.order.ShopOrder;
import com.cloud.pole.shop.model.entity.order.ShopOrderComment;
import com.cloud.pole.shop.model.entity.order.ShopOrderCommentAdmin;
import com.cloud.pole.shop.model.entity.order.ShopOrderItems;
import com.cloud.pole.shop.model.vo.order.ShopOrderCommentVo;
import com.cloud.pole.shop.service.order.OrderCommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;

/*
*2019年3月20日
*cloud-shop
*author:frank-chen
*/
@Service
@Slf4j
public class OrderCommentServiceImpl implements OrderCommentService {
	
	@Autowired 
	private ShopOrderCommentBiz orderCommentBiz;
	@Autowired 
	private ShopOrderBiz shopOrderBiz;
	@Autowired 
	private ShopOrderItemsBiz shopOrderItemsBiz;
	@Autowired 
	private ShopGoodsBiz shopGoodsBiz;	
	@Autowired 
	private IUserService userService;	
	
	@Override
	public Boolean addOrderComment(String token, Long orderItemId, Integer starNum, String commentContent) throws Exception {
		IJWTInfo jwtInfo = JwtTokenUtil.getInfoFromToken(token);
		Long userId = Long.parseLong(jwtInfo.getId());
		//验证参数是否正确
		ShopOrderItems  orderItems = shopOrderItemsBiz.selectByPrimaryKey(orderItemId);
		if(orderItems == null) {
			throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "orderItemId错误");
		}
		ShopOrder order = shopOrderBiz.selectByPrimaryKey(orderItems.getOrderSn());
		if(order == null || order.getUserId().longValue() != userId.longValue()) {
			throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "订单错误");
		}
		if(order.getOrderStatus() != ShopOrder.ORDER_STATUS_FINISH || 
		   order.getPayStatus() != ShopOrder.PAY_STATUS_YES ) {
			throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "确认收货的订单才能评论");
		}
		//验证是否评论过
		if(orderItems.getCommentStatus().intValue() == ShopOrderItems.COMMENT_STATUS_YES) {
			throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "已评论过");
		}
		//增加产品评论数量
		ShopGoods shopGoods = shopGoodsBiz.selectByPrimaryKey(orderItems.getGoodsId());
		if(shopGoods == null) {
			throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "商品id错误");
		}
		int commonMarkNum = shopGoods.getCommonMarkNum() == null ? 0 : shopGoods.getCommonMarkNum();
		int goodMarkNum = shopGoods.getGoodMarkNum() == null ? 0 : shopGoods.getGoodMarkNum();
		int badMarkNum = shopGoods.getBadMarkNum() == null ? 0 : shopGoods.getBadMarkNum();
		if(starNum.intValue() == ShopOrderComment.STAR_NUM_ONE || starNum.intValue() == ShopOrderComment.STAR_NUM_TWO) {
			badMarkNum = badMarkNum + 1;
		}
		else if(starNum.intValue() == ShopOrderComment.STAR_NUM_THREE) {
			commonMarkNum = commonMarkNum + 1;
		}
		else if(starNum.intValue() == ShopOrderComment.STAR_NUM_FOUR || starNum.intValue() == ShopOrderComment.STAR_NUM_FIVE) {
			goodMarkNum = goodMarkNum + 1;
		}
		else {
			throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "starNum错误");
		}
		shopGoods = new ShopGoods();
		shopGoods.setGoodsId(orderItems.getGoodsId());
		shopGoods.setCommonMarkNum(commonMarkNum);
		shopGoods.setGoodMarkNum(goodMarkNum);
		shopGoods.setBadMarkNum(badMarkNum);
		shopGoodsBiz.updateSelectiveById(shopGoods);
		
		orderItems.setCommentStatus(ShopOrderItems.COMMENT_STATUS_YES);
		orderItems.setCommentTime(new Date());
		shopOrderItemsBiz.updateSelectiveById(orderItems);
		
		//查询用户信息
		ApiRestResponse<BaseUserApi> apiRestResponse = userService.getBaseUser(token);
		if(apiRestResponse.getStatus() != CommonConstants.STATUS_OK_CODE) {
			throw new BaseException(apiRestResponse.getStatus(), apiRestResponse.getMessage());
		}
		BaseUserApi baseUserApi = apiRestResponse.getData();
		
		ShopOrderComment comment = new ShopOrderComment();
		comment.setCommentContent(commentContent);
		comment.setCommentStatus(ShopOrderComment.COMMENT_STATUS_OK);
		comment.setCommentTime(new Date());
		comment.setDeleteFlag(ShopOrderComment.DELETE_FLAG_NO);
		comment.setGoodsId(orderItems.getGoodsId());
		comment.setShopId(order.getShopId());
		comment.setStarNum(starNum);
		comment.setUserId(userId);
		comment.setOrderSn(order.getOrderSn());
		comment.setUserName(jwtInfo.getUniqueName());
		comment.setUserNickName(baseUserApi.getNickName());
		comment.setMerchantId(order.getMerchantId());
		orderCommentBiz.insertSelective(comment);	
		return true;
	}

	@Override
	public PageInfo<ShopOrderComment> getPageOrderCommentList(String commentType, Integer goodsId, Integer pageSize,
			Integer pageNum) throws Exception {
		// TODO Auto-generated method stub
		PageHelper.startPage(pageNum, pageSize); 
		List<ShopOrderComment> list = orderCommentBiz.selectOrderCommentListByGoodsId(goodsId, commentType);
    	PageInfo<ShopOrderComment> pageInfo = new PageInfo<ShopOrderComment>(list);
		return pageInfo;
	}

	@Override
	public PageInfo<ShopOrderCommentAdmin> selectPageShopOrderCommentAdminList(String token, Map<String, Object> map, Integer pageNo,
			Integer pageSize) throws Exception {
		IJWTInfo jwtInfo = JwtTokenUtil.getInfoFromToken(token);
		int userType = jwtInfo.getType();
		Integer merchantId = null;
		//商家管理
		if(userType == JWTInfo.USER_TYPE_MERCHANTT) {
			if(StringUtils.isEmpty(jwtInfo.getAttr1())) {
				throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "merchantId为空");
			}
			merchantId = Integer.parseInt(jwtInfo.getAttr1());
			map.put("merchantId", merchantId);
		}		
		PageHelper.startPage(pageNo, pageSize); 
		List<ShopOrderCommentAdmin> list = orderCommentBiz.selectPageShopOrderCommentAdminList(map);
    	PageInfo<ShopOrderCommentAdmin> pageInfo = new PageInfo<ShopOrderCommentAdmin>(list);
		return pageInfo;
	}

	@Override
	public Boolean deletCommentAdmin(String token, Long commentId) throws Exception {
		// TODO Auto-generated method stub
		IJWTInfo jwtInfo = JwtTokenUtil.getInfoFromToken(token);
		int userType = jwtInfo.getType();
		Integer merchantId = null;
		//商家管理
		if(userType == JWTInfo.USER_TYPE_MERCHANTT) {
			if(StringUtils.isEmpty(jwtInfo.getAttr1())) {
				throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "merchantId为空");
			}
			merchantId = Integer.parseInt(jwtInfo.getAttr1());
		}
		ShopOrderComment entity = orderCommentBiz.selectById(commentId);
		if(entity == null) {
			throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "推荐不存在");
		}
		if(userType == JWTInfo.USER_TYPE_MERCHANTT) {
			//权限控制
			if(entity.getMerchantId() != null && entity.getMerchantId().intValue() != merchantId.intValue()) {
				throw new BaseException(CommonConstants.STATUS_ERROR_CODE, "无权限操作");
			}
		}
		entity.setDeleteFlag(1);
		entity.setDeleteTime(new Date());
		orderCommentBiz.updateSelectiveById(entity);
		return true;
	}
}
